let id = 0
let baseUrl = 'http://146.56.230.35:3007/'
$(function () {
  //1、密码的验证非空、规则、输入是否正确
  $('#oldPassword').on('blur', checkOldPassword)
  $('#newPassword').on('blur', checkNewPassword)
  $('#reNewPassword').on('blur', checkReNewPassword)
  //2、获得sessionStorage中用户id
  let str = sessionStorage.getItem('USER_LOGIN')
  if (str) {
    //判断空，否则报错
    let object = JSON.parse(str)
    let data = object.data
    id = +data.id
  }
  //3、表单验证后提交修改请求
  $('.pass-btn').click(function () {
    let flag = checkOldPassword() & checkNewPassword() & checkReNewPassword()
    if (flag !== 1) {
      return
    }
    $.ajax({
      url: baseUrl + 'users/updatePassword',
      type: 'PUT',
      data: { id, userPassword: $('#newPassword').val() },
      success(res) {
        if (res.code !== 200) {
          return alert(res.msg)
        }
        if (res.data[0] === 1) {
          sessionStorage.clear()
          location = 'login.html'
        }
      },
      error(err) {
        console.error(err)
      },
      headers: {
        token: getToken().token,
      },
    })
  })
})

/**
 * 1、验证重复密码
 * @date 2021-03-26
 * @returns {any}
 */
function checkReNewPassword() {
  let res = checkNewPassword()
  let pass = $('#reNewPassword')
  let v = pass.val()
  let parent = pass.parent()
  let span = pass.next()
  span.html('')
  parent[0].className = ''
  if (!res) {
    span.html('新密码不正确')
    parent[0].className = 'error'
    return false
  }
  if ($('#newPassword').val() !== v) {
    span.html('两次输入密码不一致')
    parent[0].className = 'error'
    return false
  }
  span.html('重复密码输入正确')
  parent[0].className = 'ok'
  return true
}

/**
 * 2、验证新密码
 * @date 2021-03-26
 * @returns {any}
 */
function checkNewPassword() {
  let res = checkOldPassword()
  let pass = $('#newPassword')
  let v = pass.val()
  let parent = pass.parent()
  let span = pass.next()
  span.html('')
  parent[0].className = ''
  if (!res) {
    span.html('旧密码不正确')
    parent[0].className = 'error'
    return false
  }
  if (!v) {
    span.html('密码不能空')
    parent[0].className = 'error'
    return false
  }
  if (v.length < 6 || v.length > 12) {
    span.html('密码长度6-12位')
    parent[0].className = 'error'
    return false
  }
  //包含特殊字符
  let str = '_$()-+*&{}@!%'
  let flag = false
  for (let i = 0; i < str.length; i++) {
    let code = str[i]
    if (v.indexOf(code) !== -1) {
      flag = true
      break
    }
  }
  if (!flag) {
    span.html('密码必须包含_$()-+*&{}@!%')
    parent[0].className = 'error'
    return false
  }
  if (v === $('#oldPassword').val()) {
    span.html('新密码不能和旧密码重复')
    parent[0].className = 'error'
    return false
  }
  span.html('密码输入正确')
  parent[0].className = 'ok'
  return true
}

/**
 * 3、验证旧密码
 * @date 2021-03-26
 * @returns {any}
 */
function checkOldPassword() {
  let pass = $('#oldPassword')
  let v = pass.val()
  let parent = pass.parent()
  let span = pass.next()
  span.html('')
  parent[0].className = ''
  if (!v) {
    span.html('密码不能空')
    parent[0].className = 'error'
    return false
  }
  if (v.length < 6 || v.length > 12) {
    span.html('密码长度6-12位')
    parent[0].className = 'error'
    return false
  }
  //获得加密后的密码
  let encodePass = new MD5().hex_md5(v)
  //userPassword是密文
  let userPassword = getToken().userPassword
  if (encodePass.toLowerCase() !== userPassword.toLowerCase()) {
    span.html('输入的密码不正确')
    parent[0].className = 'error'
    return false
  }
  span.html('密码输入正确')
  parent[0].className = 'ok'
  return true
}
